package com.zx.onlineeducation.service.edu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zx.onlineeducation.common.util.R;
import com.zx.onlineeducation.service.edu.entity.CourseDO;
import com.zx.onlineeducation.service.edu.entity.vo.CourseInfoVO;
import com.zx.onlineeducation.service.edu.entity.vo.CourseQueryVO;
import com.zx.onlineeducation.service.edu.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author 郑雪
 * @since 2022-01-13
 */
@Api(tags = "课程管理")
@RestController
@RequestMapping("/edu/course")
@CrossOrigin // 解决跨域
public class CourseController {

    /**
     * 课程服务接口
     */
    @Autowired
    private CourseService courseService;

    @ApiOperation("获取课程分页列表")
    @PostMapping("/page/{current}/{size}")
    public R page(
            @ApiParam(value = "当前页")
            @PathVariable("current") Long current,
            @ApiParam(value = "每页显示条数")
            @PathVariable("size") Long size,
            @ApiParam(value = "课程查询对象")
            @RequestBody CourseQueryVO courseQueryVO) {
        if(size == null || size == 0) {
            size = 5L;
        }
        // 执行分页查询
        Page<CourseDO> page = courseService.page(current, size, courseQueryVO);
        // 获取总记录数
        long total = page.getTotal();
        // 获取数据列表
        List<CourseDO> records = page.getRecords();
        // 返回结果
        return R.ok().data("total", total).data("records", records);
    }

    @ApiOperation("添加课程基本信息")
    @PostMapping("/save")
    public R save(
            @ApiParam(value = "课程基本信息视图对象")
            @RequestBody CourseInfoVO courseInfoVO) {
        String courseId = courseService.save(courseInfoVO);
        return R.ok().data("courseId", courseId);
    }

    @ApiOperation("根据ID查询课程基本信息")
    @GetMapping("/{id}")
    public R getById(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable("id") String id) {
        CourseInfoVO courseInfoVO = courseService.getCourseInfoById(id);
        return R.ok().data("courseInfo", courseInfoVO);
    }

    @ApiOperation("修改课程基本信息")
    @PutMapping("/{id}")
    public R update(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable("id") String id,
            @ApiParam(value = "课程基本信息视图对象")
            @RequestBody CourseInfoVO courseInfoVO) {
        courseInfoVO.setId(id);
        boolean update = courseService.updateById(courseInfoVO);
        return update ? R.ok() : R.error();
    }

    @ApiOperation("发布课程")
    @PutMapping("/publish/{id}")
    public R publish(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable("id") String id) {
        CourseDO courseDO = new CourseDO();
        courseDO.setId(id);
        courseDO.setStatus("Normal");
        boolean update = courseService.updateById(courseDO);
        return update ? R.ok() : R.error();
    }

    @ApiOperation("逻辑删除课程")
    @DeleteMapping("/{id}")
    public R remove(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable("id") String id) {
        boolean remove = courseService.removeCourseById(id);
        return remove ? R.ok() :R.error();
    }
}

